import { useRequest } from 'umi';
import { useMemo } from 'react';
import { requestFunComponFiex } from '@/utils';
import { getBaseUrl } from '@/services/urlconst';
const baseUrl = getBaseUrl();

const PlanMaterialInfoServices = () => {
  /**计划列表接口 */
  const getPlanListService = () => {
    const request = useRequest(
      (requestParams?: Record<string, any>) => {
        const reqUrl = `${baseUrl}/purchasePlanController/dataGrid/${requestParams?.params}`;
        let params: any = {
          page: 1,
          rows: 10,
          ...requestParams,
          sort: 'createTime',
          order: 'desc',
          state: '0,1',
        };

        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: true,
        paginated: true,
      },
    );

    const dataSource = useMemo(() => {
      let data = request.data?.rows;
      if (data?.length) {
        return data;
      } else {
        return [];
      }
    }, [request.data?.rows]);

    return {
      getPlanListRun: (params: any) => request.run(params),
      getPlanListDataSource: dataSource,
      getPlanListLoading: request.loading,
      getPlanListTableProps: (request as any).tableProps,
    };
  };

  /**查询计划详情接口 */
  const findPlanDetailServices = () => {
    const request = useRequest(
      (requestParams?: Record<string, any>) => {
        let reqUrl = `${baseUrl}/purchasePlanController/getPlanDetails/${requestParams?.id}`;
        return requestFunComponFiex('GET', reqUrl);
      },
      {
        manual: true,
      },
    );

    return {
      findPlanDetailRun: request?.run,
      findPlanDetailLoading: request?.loading,
    };
  };

  return {
    findPlanDetailServices,
    getPlanListService,
  };
};

export { PlanMaterialInfoServices };
